/* Copyright (C) 
 * 2019 - yuelei
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

#include "server_base.hpp"
#include <fstream>
using namespace std;
using namespace webX;

/**
 * @brief 测试121
 *
 * @tparam SERVER_TYPE
 * @param server
 */
template <typename SERVER_TYPE>
void start_server(SERVER_TYPE &server){
	//向服务器增加请求资源的处理方式
	
	//处理访问/string的POST请求，返回POST的字符串
	server.resource["^/string/?$"]["POST"] = [](ostream & response,Request & request){
		//get string from istream(*request.content);
		stringstream ss;
		*request.content >> ss.rdbuf();
		string content = ss.str();

		//直接返请求结果
		response<<"HTTP/1.1 200 OK \r\nContent-Length: "<<content.length()<<"\r\n\r\n"<<content;
	};

	//proc GET return message of request
	server.resource["^/info/?$"]["GET"] = [](ostream &response, Request &request){
		stringstream content_stream;
		content_stream<<"<h1>Request</h1>";
		content_stream<<request.method<<" "<<request.path<<" HTTP/"<<request.http_version<<"<br>";
		for (auto &header: request.header){
			content_stream<<header.first<<":"<<header.second<<"<br>";
		}
		//获得content_stream的长度
		content_stream.seekp(0,ios::end);
		response<<"HTTP/1.1 200 OK\r\nContent-Length: "<<content_stream.tellp()<<"\r\n\r\n"<<content_stream.rdbuf();
	};

	//处理访问/match/[字母+数字组成的字符串]的GET请求，例如执行请求GET /match/abc123,将返回abc123
	server.resource["^/match/([0-9a-zA-Z]+)/?$"]["GET"] = [](ostream & response, Request & request){
		string number = request.path_match[1];
		response<<"HTTP/1.1 200 OK\r\nContent-Length: "<<number.length()<<"\r\n"
				<<"Content-type:text/plain"<<"\r\n"<<"qwqw:asasa"<<"\r\n\r\n"<<number;
	};

	//处理默认GET请求
	server.default_resource["^/?(.*)$"]["GET"]=[](ostream & response, Request & request){
		string filename = "web/";
		string path = request.path_match[1];

		//防止使用..来访问web目录外的目录
		size_t last_pos = path.rfind(".");
		size_t current_pos = 0;
		size_t pos;
		while((pos = path.find('.',current_pos))!=string::npos && pos!= last_pos){
			current_pos = pos;
			path.erase(pos,1);
			last_pos--;
		}
		filename += path;
		ifstream ifs;

		//简单的平台无关的文件或目录检查
		if(filename.find('.')==string::npos){//没有查找到.
			if(filename[filename.length()-1]!='/'){
				filename += '/';
			}
			filename += "index.html";
		}
		ifs.open(filename,fstream::in);
		if(ifs){
			ifs.seekg(0,ios::end);
			size_t length = ifs.tellg();
			ifs.seekg(0,ios::beg);

			//文件拷贝到response_stream中
			response<<"HTTP/1.1 200 OK\r\nContent-Length: "<<length<<"\r\n\r\n"<<ifs.rdbuf();
			ifs.close();
		}else{
			//文件不存在
			string content = "Could not open file "+filename;
			response <<"HTTP/1.1 400 Bad Request\r\nContent-Length: "<<content.length()<<"\r\n\r\n"<<content;
		}
	};
	
	server.start();
}
